package com.example.allshengmedical.experimentrun.activity;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import com.example.allshengmedical.util.FileManagerUtil;

import android.R.bool;
import android.R.string;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.os.SystemClock;
import android.util.Log;

/**
 * @author boye009 溶解数据库
 * 
 * 
 */
public class DeletDBHelper {
	final static String RongJieTableName = "RongJieDB";
	final static String ChannelIndex = "ChannelIndex";
	final static String LightNum = "LightNum";
	final static String PointIndex = "PointIndex";
	final static String Line = "Line";
	public static final int VERSION = 1;
	int i = 0;
	SQLiteDatabase database;
	File file;
	Context context;

	public DeletDBHelper(Context ctx) {
		// TODO Auto-generated constructor stub
		File file = createDBFile();
		this.context = ctx;
		this.database = SQLiteDatabase.openOrCreateDatabase(file, null);
	}

	// public DeletDBHelper(Context context, String name, CursorFactory factory,
	// int version) {
	// super(context, name, factory, version);
	// // TODO Auto-generated constructor stub
	// }
	//
	// public DeletDBHelper(Context context) {
	// // TODO Auto-generated constructor stub
	// super(context, RongJieDBName, null, VERSION);
	// }
	//
	// @Override
	// public void onCreate( ) {
	// // TODO Auto-generated method stub
	// file = createDBFile();
	// database = SQLiteDatabase.openOrCreateDatabase(file, null);
	// this.database = db;
	// StringBuilder sqlBuilder = new StringBuilder();
	// sqlBuilder.append("CREATE TABLE " + RongJieTableName + " ( ");
	// sqlBuilder
	// .append("_id Integer primary key , ChannelIndex TEXT , LightNum TEXT ,
	// Line TEXT , PointIndex TEXT )");
	// database.execSQL(sqlBuilder.toString());
	// }

	/**
	 * @param channelIndex
	 *            通道索引
	 * @param lightNum
	 *            光强值
	 * @param line
	 *            第几条
	 * @param pointIndex
	 *            第几次
	 */
	public void add(String channelIndex, String lightNum, String line, String pointIndex) {

		ContentValues contentValues = new ContentValues();
		contentValues.put(ChannelIndex, Integer.parseInt(channelIndex));
		contentValues.put(LightNum, Integer.parseInt(lightNum));
		contentValues.put(Line, Integer.parseInt(line));
		contentValues.put(PointIndex, Integer.parseInt(pointIndex));
		database.insert(RongJieTableName, null, contentValues);
		contentValues.clear();
	}

	public void close() {
		if (database != null)
			database.close();
	}

	public File createDBFile() {
		FileManagerUtil fileManagerUtil = new FileManagerUtil();
		fileManagerUtil.createDB();
		File file = new File(Environment.getExternalStorageDirectory() + "/" + "AllShengDB" + "/"
				+ CommonSQLiteHelper.DATABASE_NAME);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return file;

	}

	public ArrayList<String> queryChannel() {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = database.rawQuery("SELECT ChannelIndex FROM " + RongJieTableName, null);
		while (cursor.moveToNext()) {
			String string = cursor.getInt(cursor.getColumnIndex("ChannelIndex")) + "";
			arrayList.add(string);
		}
		// database.close();
		return arrayList;

	}

	public ArrayList<String> queryWhereChannel(int t) {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = database.rawQuery("SELECT LightNum FROM " + RongJieTableName + " where ChannelIndex = " + t,
				null);
		while (cursor.moveToNext()) {
			String string = cursor.getFloat(cursor.getColumnIndex("LightNum")) + "";
			arrayList.add(string);

		}
		try {
			database.endTransaction();
		} catch (Exception e) {
			// TODO: handle exception
		}

		// db.close();
		cursor.close();
		return arrayList;

	}

	public boolean queryWhereChannelIsHave(int t) {
		boolean ishave = false;
		Cursor cursor = database.rawQuery("SELECT LightNum FROM " + RongJieTableName + " where ChannelIndex = " + t,
				null);
		cursor.moveToFirst();
		while (cursor.moveToNext()) {
			String string = cursor.getFloat(cursor.getColumnIndex("LightNum")) + "";
			if (string != null || "".equals(string)) {
				ishave = true;
				break;
			}
		}
		try {
			database.endTransaction();
		} catch (Exception e) {
			// TODO: handle exception
		}

		cursor.close();
		return ishave;

	}

	public ArrayList<String> queryLight() {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = database.rawQuery("SELECT LightNum FROM " + RongJieTableName, null);
		while (cursor.moveToNext()) {
			String string = cursor.getFloat(cursor.getColumnIndex("LightNum")) + "";
			arrayList.add(string);
		}
		// database.close();
		return arrayList;

	}

	public ArrayList<String> queryLine(int line) {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = database.rawQuery("SELECT LightNum FROM " + RongJieTableName + " where Line = " + line, null);
		while (cursor.moveToNext()) {
			String string = cursor.getFloat(cursor.getColumnIndex("LightNum")) + "";
			arrayList.add(string);
		}
		// database.close();
		return arrayList;

	}

	public ArrayList<String> queryLine(int line, int channelIndex) {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = database.rawQuery("SELECT LightNum FROM " + RongJieTableName + " where Line = " + (line)
				+ " and ChannelIndex=" + channelIndex, null);
		while (cursor.moveToNext()) {
			arrayList.add(cursor.getFloat(cursor.getColumnIndex("LightNum")) + "");
		}
		try {
			database.endTransaction();
		} catch (Exception e) {
			// TODO: handle exception
		}
		//
		// db.close();
		cursor.close();
		return arrayList;

	}

	public ArrayList<String> queryLine() {
		ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = database.rawQuery("SELECT Line FROM " + RongJieTableName, null);
		while (cursor.moveToNext()) {
			arrayList.add(cursor.getInt(cursor.getColumnIndex("Line")) + "");
		}
		// database.close();
		return arrayList;

	}

	/**
	 * 查询第几条线第几通道第几个点
	 * 
	 * @param line
	 * @param channelIndex
	 * @param PointIndex
	 * @return
	 */
	public synchronized Float queryLine(int line, int channelIndex, int PointIndex, int channelsize,
			boolean haveChannel1, boolean haveChannel2, boolean haveChannel3, boolean haveChannel4) {
		Float LightNum = 0f;
		int id = 0;
		id = calculateID(line, channelIndex, PointIndex, channelsize, haveChannel1, haveChannel2, haveChannel3,
				haveChannel4);
		Cursor cursor = database.rawQuery("SELECT LightNum FROM " + RongJieTableName + " where _id = " + (id), null);
		// Cursor cursor = database.rawQuery("SELECT LightNum FROM " +
		// RongJieTableName + " where Line = " + (line)
		// + " and ChannelIndex=" + channelIndex + " and PointIndex=" +
		// PointIndex, null);
		if (cursor.moveToLast()) {
			LightNum = cursor.getFloat(cursor.getColumnIndex("LightNum"));
		}
		try {
			database.endTransaction();
		} catch (Exception e) {
			// TODO: handle exception
		}

		// db.close();
		cursor.close();
		return LightNum;

	}

	public float queryLine(int line, int channelIndex, int PointIndex) {
		Float LightNum = 0f;
		Cursor cursor = database.rawQuery("SELECT LightNum FROM " + RongJieTableName + " where Line = " + (line)
				+ " and ChannelIndex=" + channelIndex + " and PointIndex=" + PointIndex, null);

		if (cursor.moveToLast()) {
			LightNum = cursor.getFloat(cursor.getColumnIndex("LightNum"));
		}
		try {
			database.endTransaction();
		} catch (Exception e) {
			// TODO: handle exception
		}
		cursor.close();
		return LightNum;

	}

	private int calculateID(int line, int channelIndex, int PointIndex, int channelsize, boolean haveChannel1,
			boolean haveChannel2, boolean haveChannel3, boolean haveChannel4) {
		int id = 0;
		if (channelsize == 4) {
			id = (PointIndex - 1) * 32 * channelsize + (channelIndex - 1) * 32 + line + 1;

		} else if (channelsize == 1) {
			id = line + 1 + (PointIndex - 1) * 32;
		} else if (channelsize == 2) {
			if (haveChannel1 && haveChannel2) {
				id = (PointIndex - 1) * 32 * channelsize + (channelIndex - 1) * 32 + line + 1;
			}
			if (haveChannel1 && haveChannel3) {
				if (channelIndex == 1) {
					id = (PointIndex - 1) * 32 * channelsize + (channelIndex - 1) * 32 + line + 1;
				} else {
					id = (PointIndex - 1) * 32 * channelsize + (2 - 1) * 32 + line + 1;
				}

			}
			if (haveChannel1 && haveChannel4) {
				if (channelIndex == 1) {
					id = (PointIndex - 1) * 32 * channelsize + (0) * 32 + line + 1;
				} else {
					id = (PointIndex - 1) * 32 * channelsize + (1) * 32 + line + 1;
				}
			}
			if (haveChannel2 && haveChannel3) {
				if (channelIndex == 2) {
					id = (PointIndex - 1) * 32 * channelsize + line + 1;
				} else {
					id = (PointIndex - 1) * 32 * channelsize + (2 - 1) * 32 + line + 1;
				}
			}
			if (haveChannel2 && haveChannel4) {
				if (channelIndex == 2) {
					id = (PointIndex - 1) * 32 * channelsize + line + 1;
				} else {
					id = (PointIndex - 1) * 32 * channelsize + (2 - 1) * 32 + line + 1;
				}
			}
			if (haveChannel3 && haveChannel4) {
				if (channelIndex == 3) {
					id = (PointIndex - 1) * 32 * channelsize + line + 1;
				} else {
					id = (PointIndex - 1) * 32 * channelsize + (2 - 1) * 32 + line + 1;
				}
			}
		} else if (channelsize == 3) {
			if (haveChannel1 && haveChannel2 && haveChannel3) {
				id = (PointIndex - 1) * 32 * 3 + (channelIndex - 1) * 32 + line + 1;
			}
			if (haveChannel1 && haveChannel2 && haveChannel4) {
				if (channelIndex == 4) {
					id = (PointIndex - 1) * 32 * 3 + (3) * 32 + line + 1;
				} else {
					id = (PointIndex - 1) * 32 * 3 + (channelIndex - 1) * 32 + line + 1;
				}
			}
			if (haveChannel1 && haveChannel4 && haveChannel3) {
				if (channelIndex == 4) {
					id = (PointIndex - 1) * 32 * 3 + (2) * 32 + line + 1;
				} else if (channelIndex == 3) {
					id = (PointIndex - 1) * 32 * 3 + (1) * 32 + line + 1;
				} else {
					id = (PointIndex - 1) * 32 * 3 + (0) * 32 + line + 1;
				}
			}
			if (haveChannel4 && haveChannel2 && haveChannel3) {
				if (channelIndex == 2) {
					id = (PointIndex - 1) * 32 * 3 + (0) * 32 + line + 1;
				} else if (channelIndex == 3) {
					id = (PointIndex - 1) * 32 * 3 + (1) * 32 + line + 1;
				} else {
					id = (PointIndex - 1) * 32 * 3 + (2) * 32 + line + 1;
				}
			}
		}
		return id;
	}

	public ArrayList<String> queryPointIndexArray() {
		ArrayList<String> arrayList2 = new ArrayList<String>();
		Cursor cursor = database.rawQuery("SELECT PointIndex FROM " + RongJieTableName, null);
		while (cursor.moveToNext()) {
			arrayList2.add(cursor.getInt(cursor.getColumnIndex("PointIndex")) + "");
		}
		return arrayList2;
	}

	public int queryPointIndex() {
		int point = 0;
		// ArrayList<String> arrayList = new ArrayList<String>();
		Cursor cursor = database.rawQuery("SELECT PointIndex FROM " + RongJieTableName, null);
		if (cursor.moveToLast()) {
			point = cursor.getInt(cursor.getColumnIndex("PointIndex"));
		}

		return point;

	}

}